package com.empleartec.adf.evaluaciones.model.view;

import com.emplartec.adf.UserDataBean;
import com.emplartec.adf.evaluaciones.model.model.test.AppModuleImpl;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import oracle.adf.view.rich.component.rich.RichDocument;
import oracle.adf.view.rich.component.rich.RichForm;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.layout.RichPanelBox;
import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;

import oracle.adf.view.rich.component.rich.output.RichSpacer;

import oracle.jbo.ApplicationModule;
import oracle.jbo.client.Configuration;

public class Login {
    private String Username;
    private String Password;
    private RichForm f1;
    private RichDocument d1;
    private RichInputText it1;
    private RichInputText it2;
    private RichCommandButton cb1;
    private RichPanelGroupLayout pgl1;
    private RichPanelBox pb1;
    private RichSpacer s1;

    public void setF1(RichForm f1) {
        this.f1 = f1;
    }

    public RichForm getF1() {
        return f1;
    }

    public void setD1(RichDocument d1) {
        this.d1 = d1;
    }

    public RichDocument getD1() {
        return d1;
    }

    public void setIt1(RichInputText it1) {
        this.it1 = it1;
    }

    public RichInputText getIt1() {
        return it1;
    }

    public void setIt2(RichInputText it2) {
        this.it2 = it2;
    }

    public RichInputText getIt2() {
        return it2;
    }

    public void setCb1(RichCommandButton cb1) {
        this.cb1 = cb1;
    }

    public RichCommandButton getCb1() {
        return cb1;
    }

    public void setUsername(String Username) {
        this.Username = Username;
    }

    public String getUsername() {
        return Username;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }

    public String getPassword() {
        return Password;
    }

    public String LoginAction() throws IOException {
        FacesMessage message;
        FacesContext context = FacesContext.getCurrentInstance();
        String amDef = "com.emplartec.adf.evaluaciones.model.model.test.AppModule";
            String config = "AppModuleLocal";
            ApplicationModule ami = Configuration.createRootApplicationModule(amDef,config); 
            AppModuleImpl am = (AppModuleImpl)ami;
            UserDataBean user = am.findUser(Username, Password);
            if (user == null){
                message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "User not found", "");
                context.addMessage("", message); 
            }else{
               // message = new FacesMessage(FacesMessage.SEVERITY_INFO, "User found " + user.getUsername(), "");
               // context.getExternalContext().redirect("inicio.jspx");
               return "success";
            }
        return null;
    }

    public void setPgl1(RichPanelGroupLayout pgl1) {
        this.pgl1 = pgl1;
    }

    public RichPanelGroupLayout getPgl1() {
        return pgl1;
    }

    public void setPb1(RichPanelBox pb1) {
        this.pb1 = pb1;
    }

    public RichPanelBox getPb1() {
        return pb1;
    }

    public void setS1(RichSpacer s1) {
        this.s1 = s1;
    }

    public RichSpacer getS1() {
        return s1;
    }
}
